package com.llt.mp.controller;

import cn.hutool.core.bean.BeanUtil;
import com.llt.mp.domain.dto.PageDto;
import com.llt.mp.domain.dto.UserFormDTO;
import com.llt.mp.domain.po.User;
import com.llt.mp.domain.query.UserQuery;
import com.llt.mp.domain.vo.UserVO;
import com.llt.mp.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("users")
@RestController
@AllArgsConstructor
public class UserController {

    private final IUserService userService;

    // 添加用户
    @RequestMapping("add")
    public void saveUser(@RequestBody UserFormDTO userDTO) {
        // 1. 把DTO拷贝到PO
        User user = BeanUtil.copyProperties(userDTO, User.class);
        // 2. 新增
        userService.save(user);
    }

    // 根据复杂业务批量查询接口
    @RequestMapping("query")
    public List<UserVO> queryUserByIds(UserQuery userQuery) {
        // 1. 查询用户PO
        List<User> users = userService.queryUsers(
                userQuery.getName(), userQuery.getStatus(), userQuery.getMinBalance(), userQuery.getMaxBalance());
        // 2. 把PO拷贝到VO
        return BeanUtil.copyToList(users, UserVO.class);
    }

    // 根据 id 批量查询, 查到的用户中要带有地址
    @RequestMapping("query2")
    public List<UserVO> queryUserByIds2(@RequestParam("ids") List<Long> ids){
        return userService.queryUserByIds2(ids);
    }

    // 修改余额接口
    @RequestMapping("deduct")
    public void deductMoneyById(@Param("id") Long id, @Param("money") Integer money) {

        userService.deductMoneyById(id, money);
    }

    // 分页查询
    @RequestMapping("query3")
    public PageDto<UserVO> queryUserPages(UserQuery query) {
        return userService.queryUserPages(query);
    }

}
